package com.hqyj.module.login.controller;
import com.hqyj.module.login.dao.UserInfoMapperDao;
import com.hqyj.module.login.pojo.UserInfo;
import com.hqyj.module.login.pojo.UserInfoNew;
import com.hqyj.module.login.service.IUserInfoService;
import com.hqyj.util.MySms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/user-info")
public class UserInfoController {
    @Autowired
    MySms mySms;

    @Autowired
    IUserInfoService userInfoService;
    @Autowired
    UserInfoMapperDao userInfoMapperDao;


    @GetMapping("/login")
    public HashMap<String,Object> login(UserInfo userInfo){
        HashMap<String, Object> login = userInfoService.login(userInfo);
        return login;
    }

    @PostMapping("/emailLogin")
    public HashMap<String,Object> emailLogin(UserInfo userInfo, HttpServletRequest request){
        HttpSession session = request.getSession();
        HashMap<String, Object> map = new HashMap<>();
        String code1 =(String) session.getAttribute("code");
        if(userInfo.getUserPwd().equals(code1)){
            map.put("info","邮箱登录成功");
        }else {
            map.put("info","邮箱登录失败");
        }

        return map;
    }

    @PostMapping("/emailSend")
    public HashMap<String,Object> emailSend(String userName, HttpServletRequest request){
        HashMap<String, Object> login = userInfoService.emailSend(userName,request);
        return login;
    }

    @GetMapping("/updatePwd")
    public Integer updatePwd(UserInfoNew userInfoNew) {
        return userInfoService.updatePwd(userInfoNew);
    }


    // redis发送邮箱验证码
    @PostMapping("/redisSend")
    public HashMap<String,Object> redisSendEmail(String userName)
    {
        return userInfoService.emailRedis(userName);
    }
    //redis邮箱登录
    @PostMapping("/redisLogin")
    public HashMap<String,Object> redisLogin(UserInfo userInfo){
        HashMap<String,Object> map = new HashMap<>();
        HashMap<String, Object> map1 = userInfoService.emailLoginRedis(userInfo);
        if(map1.get("info") == "邮箱登录成功"){
            UserInfo emailUser = userInfoMapperDao.getEmailUser(userInfo.getUserName());
            if(emailUser!=null){
                map.put("emailMsg","邮箱操作成功");
                map.put("emailUserName",emailUser.getUserName());
                map.put("emailUserId",emailUser.getUserId());
            }
            else {
                map.put("emailMsg","邮箱操作失败");
            }
        }
        return map;

    }


    @GetMapping("/smsSend")
    public HashMap<String,Object> smsSend(String userName){
        HashMap<String, Object> login = mySms.sendSms(userName);
        return login;
    }

    @PostMapping("/smsLoginRedis")
    public HashMap<String,Object> smsLoginRedis(UserInfo  userInfo){
        HashMap<String, Object> login = mySms.smsLoginRedis(userInfo);
        return login;
    }

    @PostMapping("/loginPost")
    public HashMap<String,Object> loginPost(UserInfo userInfo){
        HashMap<String, Object> login = userInfoService.login(userInfo);
        return login;
    }


}